package com.bysj.sausys.activiti.listener;

import com.bysj.sausys.activiti.common.ActivitiUtil;
import com.bysj.sausys.common.AssConst;
import com.bysj.sausys.model.SysAssAnn;
import com.bysj.sausys.model.SysUser;
import com.bysj.sausys.service.ISysAssAnnService;
import com.bysj.sausys.service.ISysUserService;
import com.bysj.sausys.util.SpringContextUtil;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社团年审复核监听器
 *
 * @author WangZK
 */
public class AssAnnReviewTaskListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        String processInstanceId = delegateTask.getProcessInstanceId();
        ProcessInstance processInstance = ActivitiUtil.getProcessInstance(processInstanceId);

        String key = processInstance.getBusinessKey();
        if (StringUtils.isNotBlank(key)) {
            ISysAssAnnService sysAssAnnService = SpringContextUtil.getBean("sysAssAnnService");
            SysAssAnn sysAssAnn = sysAssAnnService.findById(Integer.parseInt(key.split("\\.")[1]));
            Map map = new HashMap(4);
            map.put("id", sysAssAnn.getId());
            map.put("editStatus", AssConst.EDITABLE.getCode());
            sysAssAnnService.updateStatus(map);

            ISysUserService sysUserService = SpringContextUtil.getBean("sysUserService");
            List<SysUser> list = sysUserService.findByAssId(sysAssAnn.getAssId());
            List<String> names = new ArrayList<>();
            if (list.size() > 0) {
                for (SysUser sysUser : list) {
                    names.add(sysUser.getUsername());
                }
            }

            delegateTask.addCandidateUsers(names);
        }
    }
}
